//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package servlet.property;

import dao.Func;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import utils.JDBCUtils;

@WebServlet({"/DoAddPropHistory"})
public class DoAddPropHistory extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public DoAddPropHistory() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        String pname = "";
        String name = "";
        String bno = "";
        String pcount = "";
        String jsonString = Func.readJSONString(request);
        JSONObject obj = JSONObject.fromObject(jsonString);
        pname = obj.get("_pname").toString();
        name = obj.get("_name").toString();
        bno = obj.get("_bno").toString();
        pcount = obj.get("_pcount").toString();
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null && !session.getAttribute("username").toString().equals("")) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

            try {
                connection = JDBCUtils.getConnection();
                String sql = "insert into asset_history values (?,?,?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
                preparedStatement.setString(2, bno);
                preparedStatement.setString(3, pname);
                preparedStatement.setString(4, pcount);
                preparedStatement.setString(5, name);
                int result = preparedStatement.executeUpdate();
                JSONObject jsonmain = new JSONObject();
                if (result != 0) {
                    response.setStatus(200);
                    jsonmain.put("code", "200");
                    jsonmain.put("msg", "updated");
                } else {
                    response.setStatus(500);
                    jsonmain.put("code", "500");
                    jsonmain.put("msg", "Error");
                }

                out.println(jsonmain);
            } catch (ClassNotFoundException var21) {
                ClassNotFoundException e = var21;
                e.printStackTrace();
            } catch (SQLException var22) {
                SQLException e = var22;
                e.printStackTrace();
            } finally {
                JDBCUtils.release((ResultSet)resultSet, preparedStatement, connection);
            }

        } else {
            response.setStatus(403);
            out.println("403 Forbidden");
        }
    }
}
